const a = document.getElementById('addUser')
const addDialog = document.getElementsByClassName('addUser-wrapper')[0]

const updateDialog = document.getElementsByClassName('updateUser-wrapper')[0]


const  searchAdminInput= document.getElementsByClassName("searchAdminText")[0];
const searchAdminBtn = document.getElementsByClassName("searchAdminBtn")[0];
a.addEventListener('click',()=>{
    addDialog.style.display = 'block'
})

const deleteUserBtn = document.getElementsByClassName('delete');
for (let i=0;i<deleteUserBtn.length;i++){
    let aid = deleteUserBtn[i].getAttribute('aid');
    deleteUserBtn[i].addEventListener('click',()=>{
        if(confirm(`是否删除id为${aid}的管理员`)){
            location.href = `http://localhost:8080/library/admin/deleteAdmin?aid=${aid}`;
            alert("删除成功！");
        }
        return false;
    });
}


var cancleBtn = document.getElementsByClassName('cancle');


cancleBtn[0].addEventListener('click',()=>{
    addDialog.style.display = 'none';
});

cancleBtn[1].addEventListener('click',()=>{
    updateDialog.style.display = 'none';
});



const updateAs = document.getElementsByClassName('update')
for(var i=0;i<updateAs.length;i++){
    updateAs[i].addEventListener('click',()=>{
        let aid = updateAs[i].getAttribute('aid');
        let username = updateAs[i].getAttribute('username');
        let password = updateAs[i].getAttribute('password');

        updateDialog.style.display = 'block';

        var updateInputs = document.querySelectorAll("#updateForm input")
        updateInputs[0].value =  aid;
        updateInputs[0].setAttribute("disabled","disabled");
        updateInputs[1].value = username;
        updateInputs[2].value = password;


        return false;
    })
}


searchAdminBtn.addEventListener('click',()=>{
    var searchAdminText = searchAdminInput.value;
    location.href = `http://localhost:8080/library/admin/adminManage?searchAdminText=${searchAdminText}`;

})